﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Globalization;
using System.IO;
using System.Collections;
using iTextSharp.text;
using System.Web.UI;
using System.Web;
using iTextSharp.text.pdf;



public partial class Main_ServiceInvoice_SalesInvoiceReport : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    DataTable dtGetFullDetail = new DataTable();
    NumberFormatInfo nfi = new CultureInfo("hi-IN", false).NumberFormat;
  
    int rowcount = 0;
    string lblInformation = "";

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
          
        }
     
       BindCompDetails();
       BindFullDetail();
       BindDetails();
    }
    protected void BindCompDetails()
    {
        DataTable dtCompDtl = DU.getdatausingprocedure("usp_get_company_details");
        string strCompDtl = "<b>Supplier</b><br />";
        string strCompNoDtl = "";
        if (dtCompDtl.Rows.Count > 0)
        {
            if (dtCompDtl.Rows[0]["compname"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["compname"].ToString() + "<br />";
                lblInvoiceFor.Text = dtCompDtl.Rows[0]["compname"].ToString();
            }
            if (dtCompDtl.Rows[0]["add1"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["add1"].ToString();
            }
            if (dtCompDtl.Rows[0]["add2"].ToString() != string.Empty)
            {
                strCompDtl += " "+ dtCompDtl.Rows[0]["add2"].ToString() + ",<br />";
            }
            if (dtCompDtl.Rows[0]["add3"].ToString() != string.Empty)
            {
                strCompDtl +=  dtCompDtl.Rows[0]["add3"].ToString();
            }
            if (dtCompDtl.Rows[0]["add4"].ToString() != string.Empty)
            {
                strCompDtl += " " + dtCompDtl.Rows[0]["add4"].ToString() + ",<br />";
            }
            if (dtCompDtl.Rows[0]["place"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["place"].ToString();
            }
            if (dtCompDtl.Rows[0]["pincode"].ToString() != string.Empty)
            {
                strCompDtl += "-"+ dtCompDtl.Rows[0]["pincode"].ToString() + "<br />";
            }
            if (dtCompDtl.Rows[0]["state"].ToString() != string.Empty)
            {
                strCompDtl += dtCompDtl.Rows[0]["state"].ToString(); 
            }
            if (dtCompDtl.Rows[0]["country"].ToString() != string.Empty)
            {
                strCompDtl += " " + dtCompDtl.Rows[0]["country"].ToString() + ".<br />";
            }
            if (dtCompDtl.Rows[0]["Phone"].ToString() != string.Empty)
            {
                strCompDtl += "Phone-" + dtCompDtl.Rows[0]["Phone"].ToString() + "<br />";
            }
            if (dtCompDtl.Rows[0]["Email"].ToString() != string.Empty)
            {
                strCompDtl += "Email-" + dtCompDtl.Rows[0]["Email"].ToString() + "<br />";
            }
            if (dtCompDtl.Rows[0]["LogoUrl"].ToString() != string.Empty)
            {
                 imgLogo.ImageUrl = dtCompDtl.Rows[0]["LogoUrl"].ToString();
                 imgLogo.Visible = true;
            }
            else
            {
                imgLogo.Visible = false;
            }
            if (dtCompDtl.Rows[0]["pan"].ToString() != string.Empty)
            {
                lblPanNo.Text = dtCompDtl.Rows[0]["pan"].ToString();
            }
            else
            {
                lblPanNo.Text = "";
                lblCompPAN.Text = "";
            }
            if (dtCompDtl.Rows[0]["vatno"].ToString() != string.Empty)
            {
                lblVATTIN.Text = dtCompDtl.Rows[0]["vatno"].ToString();
            }
            else
            {
                lblCompVat.Text = "";
                lblVATTIN.Text = "";
            }
            if (dtCompDtl.Rows[0]["sertno"].ToString() != string.Empty)
            {
                lblServiceTaxNo.Text = dtCompDtl.Rows[0]["sertno"].ToString();
            }
            else
            {
                lblServiceTaxNo.Text = "";
                lblCompServ.Text = "";
            }
        }
        lblSupplier.Text = strCompDtl;
    }
    protected void BindDetails()    
    {
        if (Request.QueryString != null)
        {
            if (Request.QueryString["INVOICEID"].ToString() != string.Empty && Request.QueryString["TRANID"].ToString() != string.Empty && Request.QueryString["ViewType"].ToString() != string.Empty)
            {
                SqlParameter[] sqlparam = {
                                              new SqlParameter("@invoiceid",Request.QueryString["INVOICEID"].ToString()),
                                              new SqlParameter("@Tranid",Request.QueryString["TRANID"].ToString())
                                          };
                lblViewType.Text = "(" + Request.QueryString["ViewType"].ToString() + ")";
                DataTable DTMain = DU.getdatausingprocedure("USP_IVC_GetSaleInvoiceDetails", sqlparam);
                string invoiceto = "";

                if (DTMain.Rows.Count > 0)
                {
                    if (DTMain.Rows[0]["billno"].ToString() != string.Empty)
                    {
                        lblBillNo.Text = DTMain.Rows[0]["billno"].ToString();
                    }
                    if (DTMain.Rows[0]["refno"].ToString() != string.Empty)
                    {
                        lblRefNo.Text = DTMain.Rows[0]["refno"].ToString();
                    }
                    if (DTMain.Rows[0]["voudate"].ToString() != string.Empty)
                    {
                        lblDate.Text = string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(DTMain.Rows[0]["voudate"].ToString()));
                    }
                    if (DTMain.Rows[0]["totalamt"].ToString() != string.Empty)
                    {
                        nfi.CurrencySymbol = "";
                        lblGrandTotal.Text = Convert.ToDouble(DTMain.Rows[0]["totalamt"].ToString()).ToString("C", nfi);
                    }
                    if (DTMain.Rows[0]["customer"].ToString() != string.Empty)
                    {
                        invoiceto += DTMain.Rows[0]["customer"].ToString() + "<br />";
                    }
                    if (DTMain.Rows[0]["Address1"].ToString() != string.Empty)
                    {
                        invoiceto += DTMain.Rows[0]["Address1"].ToString();
                    }
                    if (DTMain.Rows[0]["Address2"].ToString() != string.Empty)
                    {
                        invoiceto += " " + DTMain.Rows[0]["Address2"].ToString() + ",<br />";
                    }
                    if (DTMain.Rows[0]["Address3"].ToString() != string.Empty)
                    {
                        invoiceto += DTMain.Rows[0]["Address3"].ToString();
                    }
                    if (DTMain.Rows[0]["place"].ToString() != string.Empty)
                    {
                        invoiceto += " " + DTMain.Rows[0]["place"].ToString();
                    }
                    if (DTMain.Rows[0]["Pincode"].ToString() != string.Empty)
                    {
                        invoiceto += "-" + DTMain.Rows[0]["Pincode"].ToString() + ",<br />";
                    }
                    if (DTMain.Rows[0]["state"].ToString() != string.Empty)
                    {
                        invoiceto += DTMain.Rows[0]["state"].ToString();
                    }
                    if (DTMain.Rows[0]["Country"].ToString() != string.Empty)
                    {
                        invoiceto += " " + DTMain.Rows[0]["Country"].ToString() + "<br />";
                    }
                    if (DTMain.Rows[0]["EMAIL"].ToString() != string.Empty)
                    {
                        invoiceto += "Email-" + DTMain.Rows[0]["EMAIL"].ToString() + "<br />";
                    }
                    if (DTMain.Rows[0]["PHONE"].ToString() != string.Empty)
                    {
                        invoiceto += "Phone-" + DTMain.Rows[0]["PHONE"].ToString();
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["FIRMNAMEADDRYN"].ToString()) != true)
                    {
                        lblSupplier.Text = "";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["INFORMATIONYN"].ToString()))
                    {
                        lblInformation = DTMain.Rows[0]["INFORMATION"].ToString();
                    }
                    else
                    {
                        lblInformation = "Amount in words-";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["STREGNOYN"].ToString()) != true)
                    {
                        lblServiceTaxNo.Text = "";
                        lblCompServ.Text = "";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["CAPTIONYN"].ToString()) != true)
                    {
                        lblCaption.Text = "SALE INVOICE";
                    }
                    else
                    {
                        lblCaption.Text = DTMain.Rows[0]["BILLCAPTION"].ToString();
                    }
                    if (DTMain.Rows[0]["GTInwords"].ToString() != string.Empty)
                    {
                        lblAmountInWords.Text = lblInformation + DTMain.Rows[0]["GTInwords"].ToString();
                    }
                    if (DTMain.Rows[0]["PREPAREDBY"].ToString() != string.Empty)
                    {
                        lblpreparedby.Text = DTMain.Rows[0]["PREPAREDBY"].ToString() + "<br />";
                    }
                    else
                    {
                        lblpreparedbyHead.Text = "";
                        lblpreparedby.Text = "";
                    }
                    if (DTMain.Rows[0]["CHECKEDBY"].ToString() != string.Empty)
                    {
                        lblcheckedby.Text = DTMain.Rows[0]["CHECKEDBY"].ToString() + "<br />";
                    }
                    else
                    {
                        lblcheckedbyHead.Text = "";
                        lblcheckedby.Text = "";
                    }
                    if (DTMain.Rows[0]["SERVICEDBY"].ToString() != string.Empty)
                    {
                        lblservicedby.Text = DTMain.Rows[0]["SERVICEDBY"].ToString() + "<br />";
                    }
                    else
                    {
                        lblservicedbyHead.Text = "";
                        lblservicedby.Text = "";
                    }
                    if (DTMain.Rows[0]["Narration"].ToString() != string.Empty)
                    {
                        lblNarration.Text = DTMain.Rows[0]["Narration"].ToString() + "<br />";
                    }
                    else
                    {
                        lblNarrHead.Text = "";
                        lblNarration.Text = "";
                    }
                    if (DTMain.Rows[0]["comments"].ToString() != string.Empty)
                    {
                        lblRemarks.Text = DTMain.Rows[0]["comments"].ToString() + "<br />";
                    }
                    else
                    {
                        lblRemarksHead.Text = "";
                        lblRemarks.Text = "";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["logoyn"].ToString()) != true)
                    {
                        imgLogo.Visible = false;
                    }
                    else
                    { 
                        imgLogo.Visible = true;
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["PANYN"].ToString()) != true)
                    {
                        lblPanNo.Text = "";
                        lblCompPAN.Text = "";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["CHECKEDBYYN"].ToString()) != true)
                    {
                        lblcheckedbyHead.Text = "";
                        lblcheckedby.Text = "";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["SERVICEDBYYN"].ToString()) != true)
                    {
                        lblservicedbyHead.Text = "";
                        lblservicedby.Text = "";
                    }
                    if (Convert.ToBoolean(DTMain.Rows[0]["PREPAREDBYYN"].ToString()) != true)
                    {
                        lblpreparedbyHead.Text = "";
                        lblpreparedby.Text = "";
                    }
                    lblInvoiceTo.Text = invoiceto;
                }
            }
        }

    }
    protected void BindFullDetail() 
    {
       if (Request.QueryString != null)
        {
            if (Request.QueryString["INVOICEID"].ToString() != string.Empty && Request.QueryString["TRANID"].ToString() != string.Empty)
            {
                SqlParameter[] sqlparam = {
                                              new SqlParameter("@invoiceid",Request.QueryString["INVOICEID"].ToString())
                                           
                                          };
               dtGetFullDetail = DU.getdatausingprocedure("USP_IVC_GetSaleInvoice_FullDetail",sqlparam);
               if (dtGetFullDetail.Rows.Count > 0)
               {
                   AddNewRow();
               }
            }
       }
    }
    protected void AddNewRow()      
    {
        double pecessSum = Convert.ToDouble(dtGetFullDetail.Compute("sum(PECessAmt)", ""));
        double hecessSum = Convert.ToDouble(dtGetFullDetail.Compute("sum(HECessAmt)", ""));
        double stSum = Convert.ToDouble(dtGetFullDetail.Compute("sum(servicetaxamt)", ""));
        double disSum = Convert.ToDouble(dtGetFullDetail.Compute("sum(discount)", ""));
        double servchrgSum = Convert.ToDouble(dtGetFullDetail.Compute("sum(servicechargeamt)", ""));
        double qtySum = Convert.ToDouble(dtGetFullDetail.Compute("sum(Unit)", ""));
        double rateSum = Convert.ToDouble(dtGetFullDetail.Compute("sum(Rate)", ""));
        double grossSum = Convert.ToDouble(dtGetFullDetail.Compute("sum(GrossAmt)", ""));
        double totalSum = Convert.ToDouble(dtGetFullDetail.Compute("sum(totalamt)", ""));
        int countUOM = Convert.ToInt32(dtGetFullDetail.Compute("count(UOM)", "UOM <>''"));
        int countNatureServ = Convert.ToInt32(dtGetFullDetail.Compute("count(service)", "service<>''"));

        nfi.CurrencySymbol="";
        int tempcounter = Convert.ToInt32(dtGetFullDetail.Rows.Count);
        if (dtGetFullDetail.Rows.Count > 0)
        {
            for (int i = 0; i < tempcounter; i++)
            {
                rowcount++;
          
                HtmlTableRow row = new HtmlTableRow();
                
                HtmlTableCell cell0 = new HtmlTableCell();
                cell0.Align = "top";
                cell0.Style.Add("border", "1px solid #d6d3ce");
                cell0.Style.Add("border-left", "0px white");
                

                Label lblSNo = new Label();
                lblSNo.ID = "lblSno" + rowcount.ToString();
                lblSNo.Text = (i + 1).ToString();
             
                cell0.Controls.Add(lblSNo);

                HtmlTableCell cell1 = new HtmlTableCell();
                cell1.Align = "top";
                cell1.Style.Add("border", "1px solid #d6d3ce");
               

                Label lblNatureofService = new Label();
                lblNatureofService.ID = "lblNatureofService" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["service"].ToString() != string.Empty)
                {
                    lblNatureofService.Text = dtGetFullDetail.Rows[i]["service"].ToString();
                }
              
                cell1.Controls.Add(lblNatureofService);

                HtmlTableCell cell2 = new HtmlTableCell();
                cell2.Align = "top";
                cell2.Style.Add("border", "1px solid #d6d3ce");
                

                Label lblUom = new Label();
                lblUom.ID = "lblUom" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["UOM"].ToString() != string.Empty)
                {
                    lblUom.Text = dtGetFullDetail.Rows[i]["UOM"].ToString();
                }
                cell2.Controls.Add(lblUom);


                HtmlTableCell cell3 = new HtmlTableCell();
                cell3.Align = "right";
                cell3.Style.Add("border", "1px solid #d6d3ce");
                

                Label lblQty = new Label();
                lblQty.ID = "lblQty" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["Unit"].ToString() != string.Empty)// && Convert.ToDouble(dtGetFullDetail.Rows[i]["Unit"].ToString())>0)
                {
                    lblQty.Text = string.Format("{0:0.00}", Convert.ToDouble(dtGetFullDetail.Rows[i]["Unit"].ToString()));
                 
                }
               
                cell3.Controls.Add(lblQty);

                HtmlTableCell cell4 = new HtmlTableCell();
                cell4.Align = "right";
                cell4.Style.Add("border", "1px solid #d6d3ce");
               
                Label lblRate = new Label();
                lblRate.ID = "lblRate" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["Rate"].ToString() != string.Empty)// && Convert.ToDouble(dtGetFullDetail.Rows[i]["Rate"].ToString())>0)
                {
                    lblRate.Text = Convert.ToDouble(dtGetFullDetail.Rows[i]["Rate"].ToString()).ToString("C", nfi);
              
                }
         
                cell4.Controls.Add(lblRate);

                HtmlTableCell cell5 = new HtmlTableCell();
                cell5.Align = "right";
                cell5.Style.Add("border", "1px solid #d6d3ce");
               
                Label lblGrossAmt = new Label();
                lblGrossAmt.ID = "lblGrossAmt" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["GrossAmt"].ToString() != string.Empty)// && Convert.ToDouble(dtGetFullDetail.Rows[i]["GrossAmt"].ToString())>0 )
                {
                    lblGrossAmt.Text = Convert.ToDouble(dtGetFullDetail.Rows[i]["GrossAmt"].ToString()).ToString("C", nfi);
                  
                }
              
                cell5.Controls.Add(lblGrossAmt);

                HtmlTableCell cell6 = new HtmlTableCell();
                cell6.Align = "right";
                cell6.Style.Add("border", "1px solid #d6d3ce");
               
                Label lblSt = new Label();
                lblSt.ID = "lblSt" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["servicetaxamt"].ToString() != string.Empty)// && Convert.ToDouble(dtGetFullDetail.Rows[i]["servicetaxamt"].ToString())>0 )
                {
                 lblSt.Text = Convert.ToDouble(dtGetFullDetail.Rows[i]["servicetaxamt"].ToString()).ToString("C", nfi);
                 
                }
             
                cell6.Controls.Add(lblSt);

                HtmlTableCell cell7 = new HtmlTableCell();
                cell7.Align = "right";
                cell7.Style.Add("border", "1px solid #d6d3ce");
                
                Label lblHE = new Label();
                lblHE.ID = "lblHE" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["HECessAmt"].ToString() != string.Empty)// && Convert.ToDouble(dtGetFullDetail.Rows[i]["HECessAmt"].ToString())>0)
                {
                    lblHE.Text = Convert.ToDouble(dtGetFullDetail.Rows[i]["HECessAmt"].ToString()).ToString("C", nfi);
                  
                }
                
                cell7.Controls.Add(lblHE);

                HtmlTableCell cell8 = new HtmlTableCell();
                cell8.Align = "right";
                cell8.Style.Add("border", "1px solid #d6d3ce");

                Label lblPE = new Label();
                lblPE.ID = "lblPE" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["PECessAmt"].ToString() != string.Empty)// && Convert.ToDouble(dtGetFullDetail.Rows[i]["PECessAmt"].ToString())>0)
                {
                    lblPE.Text = Convert.ToDouble(dtGetFullDetail.Rows[i]["PECessAmt"].ToString()).ToString("C", nfi);
                  
                }
                
                cell8.Controls.Add(lblPE);

                HtmlTableCell cell9 = new HtmlTableCell();
                cell9.Align = "right";
                cell9.Style.Add("border", "1px solid #d6d3ce");
             
                Label lblsrvchrg = new Label();
                lblsrvchrg.ID = "lblsrvchrg" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["servicechargeamt"].ToString() != string.Empty)// && Convert.ToDouble(dtGetFullDetail.Rows[i]["servicechargeamt"].ToString())>0)
                {
                    lblsrvchrg.Text = Convert.ToDouble(dtGetFullDetail.Rows[i]["servicechargeamt"].ToString()).ToString("C", nfi);
              
                }
                
                cell9.Controls.Add(lblsrvchrg);

                HtmlTableCell cell10 = new HtmlTableCell();
                cell10.Align = "right";
                cell10.Style.Add("border", "1px solid #d6d3ce");

                Label lblDis = new Label();
                lblDis.ID = "lblDis" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["discount"].ToString() != string.Empty)// && Convert.ToDouble(dtGetFullDetail.Rows[i]["discount"].ToString())>0)
                {
                    lblDis.Text = Convert.ToDouble(dtGetFullDetail.Rows[i]["discount"].ToString()).ToString("C", nfi);
                  
                }
                
                cell10.Controls.Add(lblDis);

                HtmlTableCell cell11 = new HtmlTableCell();
                cell11.Align = "right";
                cell11.Style.Add("border", "1px solid #d6d3ce");
                cell11.Style.Add("border-right", "0px white");
                
                Label lblTtlAmt = new Label();
                lblTtlAmt.ID = "lblTtlAmt" + rowcount.ToString();
                if (dtGetFullDetail.Rows[i]["totalamt"].ToString() != string.Empty)// && Convert.ToDouble(dtGetFullDetail.Rows[i]["totalamt"].ToString())>0)
                {
                    lblTtlAmt.Text = Convert.ToDouble(Convert.ToDouble(Convert.ToDouble(dtGetFullDetail.Rows[i]["GrossAmt"].ToString()) + Convert.ToDouble(dtGetFullDetail.Rows[i]["servicetaxamt"].ToString()) + Convert.ToDouble(dtGetFullDetail.Rows[i]["HECessAmt"].ToString()) + Convert.ToDouble(dtGetFullDetail.Rows[i]["PECessAmt"].ToString()) + Convert.ToDouble(dtGetFullDetail.Rows[i]["servicechargeamt"].ToString()) - Convert.ToDouble(dtGetFullDetail.Rows[i]["discount"].ToString()))).ToString("C", nfi); //string.Format("{0:0.00}", Convert.ToDouble(dtGetFullDetail.Rows[i]["totalamt"].ToString())));
                   
                }
               
                cell11.Controls.Add(lblTtlAmt);

                row.Cells.Add(cell0);
                if (countNatureServ > 0)
                {
                    row.Cells.Add(cell1);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdNature")).Style.Add("display", "none");
                }
                if (countUOM > 0)
                {
                    row.Cells.Add(cell2);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdUnit")).Style.Add("display", "none");
                }
                if (qtySum > 0)
                {
                    row.Cells.Add(cell3);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdQty")).Style.Add("display", "none");
                }
                if (rateSum > 0)
                {
                    row.Cells.Add(cell4);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdRate")).Style.Add("display", "none");
                }
                if (grossSum > 0)
                {
                    row.Cells.Add(cell5);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdGrossAmt")).Style.Add("display", "none");
                }
                if (stSum > 0)
                {
                    row.Cells.Add(cell6);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdST")).Style.Add("display", "none");
                }
                if (hecessSum > 0)
                {
                    row.Cells.Add(cell7);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdHE")).Style.Add("display", "none");
                }
                if (pecessSum > 0)
                {
                    row.Cells.Add(cell8);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdPE")).Style.Add("display", "none");
                }
                if (servchrgSum > 0)
                {
                    row.Cells.Add(cell9);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdServ")).Style.Add("display", "none");
                }
                if (disSum > 0)
                {
                    row.Cells.Add(cell10);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdDis")).Style.Add("display", "none");
                }
                if (totalSum > 0)
                {
                    row.Cells.Add(cell11);
                }
                else
                {
                    ((HtmlTableCell)tblDetail.Rows[0].FindControl("tdTotal")).Style.Add("display", "none");
                }
                tblDetail.Rows.Add(row);
                hidControlCounter.Value = rowcount.ToString();
            }
        }
            int rslt = 20 - tempcounter;
            if (rslt > 0)
            {
                for (int j = 0; j <= rslt; j++)
                {
                    HtmlTableRow row1 = new HtmlTableRow();
                  
                    HtmlTableCell cel0 = new HtmlTableCell();
                    cel0.Align = "top";
                    cel0.InnerHtml = "&nbsp;";
                    cel0.BorderColor = "#ffffff";

                    HtmlTableCell cel1 = new HtmlTableCell();
                    cel1.Align = "top";
                    cel1.InnerHtml = "&nbsp;";
                    cel1.BorderColor = "#ffffff";

                    HtmlTableCell cel2 = new HtmlTableCell();
                    cel2.Align = "top";
                    cel2.InnerHtml = "&nbsp;";
                    cel2.BorderColor = "#ffffff";

                    HtmlTableCell cel3 = new HtmlTableCell();
                    cel3.Align = "right";
                    cel3.InnerHtml = "&nbsp;";
                    cel3.BorderColor = "#ffffff";

                    HtmlTableCell cel4 = new HtmlTableCell();
                    cel4.Align = "right";
                    cel4.InnerHtml = "&nbsp;";
                    cel4.BorderColor = "#ffffff";

                    HtmlTableCell cel5 = new HtmlTableCell();
                    cel5.Align = "right";
                    cel5.InnerHtml = "&nbsp;";
                    cel5.BorderColor = "#ffffff";

                    HtmlTableCell cel6 = new HtmlTableCell();
                    cel6.Align = "right";
                    cel6.InnerHtml = "&nbsp;";
                    cel6.BorderColor = "#ffffff";

                    HtmlTableCell cel7 = new HtmlTableCell();
                    cel7.Align = "right";
                    cel7.InnerHtml = "&nbsp;";
                    cel7.BorderColor = "#ffffff";

                    HtmlTableCell cel8 = new HtmlTableCell();
                    cel8.Align = "right";
                    cel8.InnerHtml = "&nbsp;";
                    cel8.BorderColor = "#ffffff";

                    HtmlTableCell cel9 = new HtmlTableCell();
                    cel9.Align = "right";
                    cel9.InnerHtml = "&nbsp;";
                    cel9.BorderColor = "#ffffff";

                    HtmlTableCell cel10 = new HtmlTableCell();
                    cel10.Align = "right";
                    cel10.InnerHtml = "&nbsp;";
                    cel10.BorderColor = "#ffffff";

                    HtmlTableCell cel11 = new HtmlTableCell();
                    cel11.Align = "right";
                    cel11.InnerHtml = "&nbsp;";
                    cel11.BorderColor = "#ffffff";

                    row1.Cells.Add(cel0);
                    if (countNatureServ> 0)
                    {
                        row1.Cells.Add(cel1);
                    }
                    if (countUOM > 0)
                    {
                        row1.Cells.Add(cel2);
                    }
                    if (qtySum > 0)
                    {
                        row1.Cells.Add(cel3);
                    }
                    if (rateSum > 0)
                    {
                        row1.Cells.Add(cel4);
                    }
                    if (grossSum > 0)
                    {
                        row1.Cells.Add(cel5);
                    }
                    if (stSum > 0)
                    {
                        row1.Cells.Add(cel6);
                    }
                    if (hecessSum > 0)
                    {
                        row1.Cells.Add(cel7);
                    }
                    if (pecessSum > 0)
                    {
                        row1.Cells.Add(cel8);
                    }
                    if (servchrgSum > 0)
                    {
                        row1.Cells.Add(cel9);
                    }
                    if (disSum > 0)
                    {
                        row1.Cells.Add(cel10);
                    }
                    if (totalSum > 0)
                    {
                        row1.Cells.Add(cel11);
                    }
                    tblDetail.Rows.Add(row1);
                }

            }
    }

    private void PrepareGridViewForExport(Control gv)
    {
        HiddenField hidControl = new HiddenField();
        for (int i = 0; i < gv.Controls.Count; i++)
        {
            if (gv.Controls[i].GetType() == typeof(HiddenField))
            {
                gv.Controls.Remove(gv.Controls[i]);
            }
            if (gv.Controls[i].HasControls())
            {
                PrepareGridViewForExport(gv.Controls[i]);
            }
        }
    }
    //protected void lkbtnpdf_Click(object sender, EventArgs e)
    //{
        //Response.ContentType = "application/pdf";
        //Response.AddHeader("content-disposition", "attachment;filename=SaleInvoice.pdf");
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        //StringWriter sw = new StringWriter();
        //HtmlTextWriter htw = new HtmlTextWriter(sw);
        //pnlpdf.RenderControl(htw);
        //iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
        //ArrayList p = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(sw.ToString()), styles);
        //pdfDoc.Open();

        //for (int k = 0; k < p.Count; k++)
        //{
        //    pdfDoc.Add((iTextSharp.text.IElement)p[k]);
        //}

        //pdfDoc.Close();
        //Response.Write(pdfDoc);
        //Response.End();
        //Response.ContentType = "application/pdf";
        //Response.AddHeader("content-disposition", "attachment;filename=SalesInvoice.pdf");
        //Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //Document document = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        //try
        //{
        //    StringWriter sw = new StringWriter();
        //    HtmlTextWriter htw = new HtmlTextWriter(sw);
        //    PrepareGridViewForExport(pnlpdf);
        //    pnlpdf.RenderControl(htw);
        //    string strDataBody = "<html><head></head><body>" + htw.InnerWriter.ToString() + "</body></html>";
        //    iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();

        //    PdfWriter.GetInstance(document, Response.OutputStream);
        //    document.Open();
        //    ArrayList objects;
        //    document.NewPage();
        //    Chunk c = new Chunk(Session["compname"].ToString() + "\n", FontFactory.GetFont("Verdana", 10));
        //    Paragraph p = new Paragraph();
        //    p.Alignment = Element.ALIGN_CENTER;
        //    p.Add(c);
           
        //    document.Add(p);
           
        //    Paragraph p2 = new Paragraph();
        //    objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(strDataBody), styles);
        //    for (int k = 0; k < objects.Count; k++)
        //    {
        //        p2.Add((iTextSharp.text.IElement)objects[k]);
        //    }
        //    p2.Font = FontFactory.GetFont("Verdana", 2);
        //    document.Add(p2);
        //    document.Close();
        //    Response.Write(document);
        //    Response.End();
        //}
        //catch (Exception ex)
        //{
        //    throw ex;
        //}
    //}
}
